package com.dwjm.goods;

import com.dwjm.ConnectionDatabaseForMySQLUserJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodDaolpml implements GoodDao {

    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    public GoodDaolpml() {
        connection = ConnectionDatabaseForMySQLUserJdbc.getConnection();
    }
    @Override
    public int addGood(Goods goods) {
        try {
            pstmt = connection.prepareStatement("insert into t_gods(title, price, descr, img, state, mid) values(?, ?, ?, ?, ?, ?)");
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, 0);
            pstmt.setInt(6, goods.getMid());
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0; // 三目运算符
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }



    @Override
    public List<Goods> findAllGoods(int page, int size) {
        List<Goods> list = new ArrayList<>();
        try {
            // SQL Server 分页查询必须带 ORDER BY
            String sql = "SELECT * FROM (" +
                    " SELECT ROW_NUMBER() OVER (ORDER BY id) AS rownum, id, title, price, descr, img, state, mid" +
                    " FROM t_gods" +
                    ") AS temp" +
                    " WHERE temp.rownum BETWEEN ? AND ?";

            pstmt = connection.prepareStatement(sql);

            int start = (page - 1) * size + 1;
            int end = page * size;

            pstmt.setInt(1, start); // 第几条开始
            pstmt.setInt(2, end);   // 第几条结束

            rset = pstmt.executeQuery();
            while (rset.next()) {
                Goods goods = new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
                list.add(goods);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int deleteGood(Integer id) {
        try {
            pstmt = connection.prepareStatement("update  t_gods set state = 1 where id = ?");
            pstmt.setInt(1, id);
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Goods> queryByCondition(String keyword) {
        List<Goods> list = new ArrayList<>();
        try {
            pstmt = connection.prepareStatement("select id, title, price, descr, img, state, mid from t_gods where title like ?");
            pstmt.setString(1, "%" + keyword + "%");
            rset = pstmt.executeQuery();
            Goods goods = null;
            while (rset.next()) {
                 goods = new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
                list.add(goods);
            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Goods getById(Integer id) {
        try {
            pstmt = connection.prepareStatement("SELECT * FROM t_gods WHERE id = ?");
            pstmt.setInt(1, id);
            rset = pstmt.executeQuery();
            if (rset.next()) {
                return new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;//
    }

    @Override
    public int updateGood(Goods goods) {
        try {
            pstmt = connection.prepareStatement(
                    "UPDATE t_gods SET title = ?, price = ?, descr = ?, img = ?, state = ? WHERE id = ?"
            );
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, goods.getState());
            pstmt.setInt(6, goods.getId());
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean batchDownGoods(String[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }

        // 构建 SQL 语句
        String sql = "UPDATE t_gods SET state = 1 WHERE id IN (" + String.join(",", Collections.nCopies(ids.length, "?")) + ")";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            // 设置 PreparedStatement 参数
            for (int i = 0; i < ids.length; i++) {
                pstmt.setInt(i + 1, Integer.parseInt(ids[i]));
            }

            int count = pstmt.executeUpdate();
            return count > 0;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
